<template>
  <div class="app-container">
    <!-- 操作 -->
    <el-row :gutter="20">
      <el-col :span="9">
        <el-input v-model="query.name" placeholder="请输入资源名称查找" @keyup.enter.native="searchData()" />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="searchData()">查询</el-button>
        <el-tooltip class="item" effect="dark" content="添加数据" placement="top-start">
          <el-button type="primary" @click="showDialog = true">添加</el-button>
        </el-tooltip>
      </el-col>
    </el-row>

    <!-- 表格数据 -->
    <el-table :data="tableData" style="width: 100%; margin: 20px 0" stripe border :default-sort="{ prop: 'createTime', order: 'descending' }">
      <el-table-column align="center" label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + addIndex }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="资源名称" prop="name" width="150"> </el-table-column>
      <el-table-column align="center" label="资源类型" prop="typeName" width="100" :filters="typeList" :filter-method="filterType" filter-placement="bottom-end"> </el-table-column>
      <el-table-column align="center" label="资源地址" prop="path"> </el-table-column>
      <el-table-column align="center" label="Logo地址" prop="picUrl" width="400"> </el-table-column>
      <el-table-column align="center" label="描述" prop="description"> </el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime" sortable> </el-table-column>
      <el-table-column align="center" label="更新时间" prop="updateTime" sortable> </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :total="total" :current-page.sync="query.current" :page-size="query.pageSize" :page-sizes="[10, 20, 30, 40]" background layout="total, sizes, prev, pager, next, jumper" style="text-align: center" />

    <!-- 弹框 -->
    <el-dialog :title="getTitle" :visible.sync="showDialog" :close-on-click-modal="false" @close="closeDialog('resourcesForm')" @close-on-press-escape="closeDialog('resourcesForm')">
      <el-form ref="resourcesForm" :model="resourcesForm" label-width="120px" :rules="rules">
        <el-form-item label="资源名称" prop="name">
          <el-input v-model="resourcesForm.name" placeholder="请输入资源名称" />
        </el-form-item>
        <el-form-item label="资源地址" prop="path">
          <el-input v-model="resourcesForm.path" placeholder="请输入资源地址" />
        </el-form-item>
        <el-form-item label="资源类型" prop="typeId">
          <el-select v-model="resourcesForm.typeId" placeholder="请选择资源类型" style="width: 100%">
            <el-option v-for="item in typeList" :label="item.text" :value="item.value" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="Logo地址" prop="picUrl">
          <el-input v-model="resourcesForm.picUrl" placeholder="请输入Logo地址" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="resourcesForm.description" type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="closeDialog('resourcesForm')">取消</el-button>
        <el-button v-if="getId" type="primary" @click="update('resourcesForm')">更新</el-button>
        <el-button v-else type="primary" @click="save('resourcesForm')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import resourcesApi from '@/api/resources.js'
import typeApi from '@/api/type'
export default {
  name: 'Resources',
  data() {
    return {
      query: {
        pageSize: 10,
        current: 1,
        name: '' //查询项
      },
      total: 0,

      // 分类列表
      typeList: [],
      // 表格
      tableData: [], // 数据
      addIndex: 1, // 序号

      // 新增/编辑弹框
      showDialog: false,
      resourcesForm: {
        id: '',
        name: '',
        path: '',
        typeId: '',
        picUrl: '',
        description: ''
      },
      // 表单验证
      rules: {
        name: [{ required: true, message: '资源名称不能为空', trigger: 'blur' }],
        path: [{ required: true, message: '资源地址不能为空', trigger: 'blur' }],
        typeId: [{ required: true, message: '资源类型不能为空', trigger: 'change' }],
        picUrl: [{ required: true, message: 'Logo地址不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 弹框标题
    getTitle: function () {
      if (this.resourcesForm.id !== '') {
        return '编辑'
      } else {
        return '添加'
      }
    },
    // 展示的按钮
    getId: function () {
      if (this.resourcesForm.id !== '') {
        return true
      } else {
        return false
      }
    }
  },
  created() {
    this.getTypelist()
    this.list()
  },
  methods: {
    // 查询
    searchData() {
      this.query.current = 1
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 页码查询
    handleCurrentChange(val) {
      this.query.current = val
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 显示条数查询
    handleSizeChange(val) {
      this.query.pageSize = val
      this.query.current = 1
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 标签筛选
    filterType(value, row) {
      return row.typeId === value
    },

    // 列表
    list() {
      resourcesApi.list(this.query).then(response => {
        if (response.code === 20000) {
          let data = response.data.data
          this.tableData = data.records

          this.query.current = data.current
          this.query.pageSize = data.size
          this.total = data.total
        } else {
          this.$message.error(response.message)
        }
      })
    },

    // 分类列表
    getTypelist() {
      typeApi.list().then(response => {
        if (response.code === 20000) {
          response.data.data.filter(item => {
            if (item.level === '2') {
              this.typeList.push({
                text: item.name,
                value: item.id
              })
            }
          })
        } else {
          this.$message.error(response.message)
        }
      })
    },

    // 删除
    handleDel(id) {
      this.$confirm('此操作将永久删除被选中的用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const form = new FormData()
        form.append('id', id)

        resourcesApi.delById(form).then(response => {
          if (response.code === 20000) {
            this.$message.success(response.message)
            //刷新页面
            this.list()
          } else {
            this.$message.error(response.message)
          }
        })
      })
    },

    // 添加
    save(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const form = new FormData()
          form.append('id', this.resourcesForm.id)
          form.append('name', this.resourcesForm.name)
          form.append('typeId', this.resourcesForm.typeId)
          form.append('path', this.resourcesForm.path)
          form.append('picUrl', this.resourcesForm.picUrl)
          form.append('description', this.resourcesForm.description)

          resourcesApi.save(form).then(response => {
            if (response.code === 20000) {
              this.$message.success(response.message)
              // 关闭弹框
              this.showDialog = false
              //刷新页面
              this.list()
            } else {
              this.$message.error(response.message)
            }
          })
        }
      })
    },

    // 编辑按钮
    edit(row) {
      this.showDialog = true
      this.resourcesForm.id = row.id
      this.resourcesForm.name = row.name
      this.resourcesForm.typeId = row.typeId
      this.resourcesForm.path = row.path
      this.resourcesForm.picUrl = row.picUrl
      this.resourcesForm.description = row.description
    },

    // 确认更新用户
    update(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const form = new FormData()
          form.append('id', this.resourcesForm.id)
          form.append('name', this.resourcesForm.name)
          form.append('typeId', this.resourcesForm.typeId)
          form.append('path', this.resourcesForm.path)
          form.append('picUrl', this.resourcesForm.picUrl)
          form.append('description', this.resourcesForm.description)

          resourcesApi.update(form).then(response => {
            if (response.code === 20000) {
              this.$message.success(response.message)
              // 关闭弹框
              this.showDialog = false
              //刷新页面
              this.list()
            } else {
              this.$message.error(response.message)
            }
          })
        }
      })
    },

    // 关闭弹框
    closeDialog(formName) {
      this.$refs[formName].resetFields()
      this.showDialog = false
      this.resourcesForm.id = ''
      this.resourcesForm.name = ''
      this.resourcesForm.typeId = ''
      this.resourcesForm.path = ''
      this.resourcesForm.picUrl = ''
      this.resourcesForm.description = ''
    }
  }
}
</script>
